/*
 * @Descripttion: 
 * @Author: cuidm
 * @Date: 2022-07-28 14:22:54
 * @LastEditors: cuidm
 * @LastEditTime: 2022-08-12 22:53:29
 */
import React, { useState, useEffect } from "react";
import { Modal, Form, Button, Input, Switch , Select, message, Space } from "antd";
import { HcpIconSelect } from "@/components";
import cmdbres from "@/data/cmdbres";
import request from "@/utils/request";
import regexpUtil from "@/utils/regexpUtil";
import hcpUtils from "@/utils/hcpUtils";

const { uniqueIdentificationRegexp } = regexpUtil;
const user = hcpUtils.getUser();

const CreateModel = ({ visible, setVisible, onCreateFinish, edge }) => {
	const [form] = Form.useForm();
	const [loading, setLoading] = useState(false);

	const onFinish = async (values) => {
		setLoading(true);
		const model = edge.item.getModel();
		let res = await request({
			...cmdbres.createMainLine,
			data: {
				asstModuleCode: model.destModuleCode,
				asstModuleId: model.destModuleId,
				author: user.staffName,
				...values,
			}
		});
		setLoading(false);
		if (res && res.code == 200) {
			setVisible(false);
			message.success("新建层级成功！");
			if (typeof onCreateFinish == "function") onCreateFinish(values);
		} else {
			if(res) message.error(res.msg);
		}
	};

	const onCancel = () => {
		setVisible(false);
	}

	return (
		<div>
			<Modal
				title="新建层级"
				className="hcp-modal-sm"
				visible={visible}
				footer={null}
				onCancel={onCancel}
				destroyOnClose
				maskClosable={false}
				afterClose={()=>{form.resetFields()}}
			>
				<Form
					form={form}
					labelCol={{ span: 5 }}
					wrapperCol={{ span: 18 }}
					name="basic"
					onFinish={onFinish}
				>
					<Form.Item 
						label="唯一标识"
						name="moduleCode"
						required
						rules={[
							{
								required: true,
								message: '名称不能为空',
							},
							uniqueIdentificationRegexp(256),
						]}
						extra={
							<p style={{ color: "#999999" }}>
								请填写以字母开头，数字、字母、下划线，横线的组合
							</p>
						}
						style={{ marginBottom: 5 }}
					  >
						<Input placeholder="请输入唯一标识" maxLength={256} />
					</Form.Item>
					<Form.Item label="名称" name="moduleName" required>
						<Input placeholder="请输入名称" maxLength={64} />
					</Form.Item>
					<Form.Item
						name="moduleIcon"
						label="图标"
						rules={[ { required: true, message: '请选择图标' }]}
					>
						<HcpIconSelect placeholder="点击选择图标" />
					</Form.Item>
					<div className="hcp-modal-body-footer">
						<Form.Item wrapperCol={{ span: 24 }} >
							<Button onClick={onCancel}>取消</Button>
							<Button type="primary" htmlType="submit" loading={loading}>提交</Button>
						</Form.Item>
					</div>
				</Form>
			</Modal>
		</div>
	)
}

export default CreateModel;